{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Total Probability\n",
    "\n",
    "This code is based on a medical diagnostic use of Bayes' rule.\n",
    "\n",
    "The `total_probability` function automatically calculates the total probability of a positive test result, given the prior probability of someone having the disease, `p_disease`, and the conditional probability of the test being positive given whether a person being tested has or does not have the disease `p_pos_given_disease` and `p_pos_given_no_disease`. In our usual math notation, these would look like:\n",
    "\n",
    "$$P(Disease)$$\n",
    "$$P(Positive  Test | Disease)$$\n",
    "$$P(Positive Test | \\neg Disease)$$\n",
    "\n",
    "This function takes in those three probabilities as input and should return a single value; the total probability of a positive test. It will be up to you to code this functionality!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TODO: Complete `total_probability`\n",
    "Complete the function so that it returns the total probability of a disease test, returning a positive result."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your function returned that the total probability is: 0.6\n"
     ]
    }
   ],
   "source": [
    "# The inputs to total probability are given descriptive names\n",
    "\n",
    "def total_probability(p_c, p_pos_given_c, p_pos_given_no_disease):\n",
    "    \n",
    "    ## TODO: Change the value of total so that it calculates the \n",
    "    ## total probability of a positive test result\n",
    "    ## You may use other variable in this function as well as long\n",
    "    ## as total is correct\n",
    "    total = p_disease * p_pos_given_disease + (1- p_disease) * p_pos_given_no_disease \n",
    "    \n",
    "    return total\n",
    "\n",
    "    \n",
    "## TODO: Change these test values, run your function, and write print statements to test your code\n",
    "## Answer the question: what is the probability of a positive test result given the following values?\n",
    "p_disease = 0.2\n",
    "p_pos_given_disease= 0.6\n",
    "p_pos_given_no_disease= 0.6\n",
    "\n",
    "tot = total_probability(p_disease, p_pos_given_disease, p_pos_given_no_disease)\n",
    "print('Your function returned that the total probability is: '+str(tot))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Testing Cell\n",
    "\n",
    "Run this cell and it will compare the output of your function with the correct, expected output.\n",
    "\n",
    "**Assertions**\n",
    "\n",
    "This cell is using something called `assertions` in Python, which are statements that check the validity of code. In this case, the assertion checks that the output of your function is equal to the expected total_probability and then prints out feedback!\n",
    "\n",
    "**Your code should pass all tests and work for any given probabilities.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "That's right!\n"
     ]
    }
   ],
   "source": [
    "# Test code - do not change\n",
    "import solution\n",
    "\n",
    "p_disease = 0.1\n",
    "p_pos_given_disease= 0.9\n",
    "p_pos_given_no_disease= 0.2\n",
    "\n",
    "# This line calls your function and stores the output\n",
    "total = total_probability(p_disease, p_pos_given_disease, p_pos_given_no_disease)\n",
    "correct_total = solution.total_probability(p_disease, p_pos_given_disease, p_pos_given_no_disease)\n",
    "\n",
    "# Assertion, comparison test\n",
    "try:\n",
    "    assert(abs(total - correct_total) < 0.0001)\n",
    "    print('That\\'s right!')\n",
    "except:\n",
    "    print ('Your code returned that the total probability is: ' +str(total) \n",
    "           + ', which does not match the correct value.')"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
